/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.blackup;

import br.com.barrientosjunior.blackup.action.ExecuteBackupAction;
import br.com.barrientosjunior.blackup.action.ExecuteRecoverBackupAction;
import br.com.barrientosjunior.blackup.action.ExecuteRecoverEncryptedBackupAction;
import br.com.barrientosjunior.blackup.persistence.PreferencesManager;
import br.com.barrientosjunior.blackup.swing.FileJFrame;
import br.com.barrientosjunior.blackup.swing.MainJFrame;
import java.util.Vector;

/**
 *
 * @author Junior
 */
public class MainManager {
    MainJFrame mainFrame = null;

    PreferencesManager.AppPreferences prefs = null;
    
    Vector<br.com.barrientosjunior.blackup.model.File> files = null;

    FileJFrame fileFrame = null;

    public Vector<br.com.barrientosjunior.blackup.model.File> getFiles() {
        return files;
    }

    public MainJFrame getMainFrame() {
        return mainFrame;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        new MainManager();
    }

    public MainManager() {

        try {
            prefs = PreferencesManager.loadPreferences();
        } catch (Exception ex) {
            
        }

        fileFrame = new FileJFrame(this);
        mainFrame = new MainJFrame(this);
        mainFrame.pack();
        mainFrame.getjList1().setListData(prefs.getFiles());
        files = prefs.getFiles();
        mainFrame.setVisible(true);
    }

    public void openAddFile() {
        fileFrame.setVisible(true);

        this.resetFileFrame();
    }

    public void addFile(br.com.barrientosjunior.blackup.model.File file){
        files.add(file);

        mainFrame.getjList1().setListData(files);
        try {
            PreferencesManager.savePreferences(prefs);
        } catch (Exception ex) {

        }
    }

    public void resetFileFrame(){
        fileFrame.getDestinoJTextField().setText("");
        fileFrame.getOrigemJTextField().setText("");
        fileFrame.getjTextFieldPass().setText("");
    }

    public void removeFile(){
        int i = mainFrame.getjList1().getSelectedIndex();
        files.remove(i);
        mainFrame.getjList1().setListData(files);
        try {
            PreferencesManager.savePreferences(prefs);
        } catch (Exception ex) {

        }
    }

    public void executeBackup(){
         Runnable action = new ExecuteBackupAction(this);
         Thread t = new Thread(action);
         t.start();
    }

    public void closeFileFrame(){
        this.fileFrame.setVisible(false);
    }

    public void executeRecovering() {
        int i = mainFrame.getjList1().getSelectedIndex();
        br.com.barrientosjunior.blackup.model.File selectedFile = files.get(i);
        mainFrame.getjLabelStatusValue().setText("Recuperação iniciada...");
        if(selectedFile.isEncrypted()){
            Runnable recover = new ExecuteRecoverEncryptedBackupAction(this, selectedFile);
            Thread t = new Thread(recover);
            t.start();
        }else{
            Runnable recover = new ExecuteRecoverBackupAction(this, selectedFile);
            Thread t = new Thread(recover);
            t.start();
        }
    }
}
